/**
 *
 * @Title: SignInActivity.java
 * @Package com.idrem.views.bf
 * @Description: TODO(用一句话描述该文件做什么)
 * @author WangZhengCai
 * @date 2012 2012-11-13 下午8:07:59
 * @version V1.0
 *
 */
package com.idrem.views.bf;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.idrem.R;

/**
 * 
 * @ClassName: SignInActivity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WangZhengCai
 * @date 2012 2012-11-13 下午8:07:59
 * 
 * 
 */
public class SignInActivity extends Activity implements LocationListener  {

	private Button btnSignIn = null;
	private WebView webview;
	private boolean isAdded = false;
	private boolean isLoaded = false;
	private LocationManager lm;
	private String provider;
	private Location location;
	private String TAG = "SignInActivity:";
	

	void showToast(CharSequence msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_in);
		webview = (WebView)findViewById(R.id.webView1);
		btnSignIn = (Button) findViewById(R.id.btn_commit);
		btnSignIn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showToast("签到完成");

			}
		});
		initLocation();
	}
	
	private void loadMap() {
		Log.d(TAG, "==loadMap==");
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setBuiltInZoomControls(false);
		webview.getSettings().setAppCacheEnabled(true);
		webview.getSettings().setGeolocationEnabled(true);
		webview.getSettings().setSupportZoom(true);
		webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webview.getSettings().setLightTouchEnabled(true);
		webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
		webview.setClickable(true);
		webview.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				Log.d(TAG, "==onPageFinished==");
				super.onPageFinished(view, url);
				isLoaded = true;
				updataGpsWidthLocation(location);
			}
			
		});
		Log.d(TAG, "==loadUrl==");
		webview.loadUrl("file:///android_asset/map.html");
	}
	
	@Override
	protected void onResume() {
		Log.d(TAG, "==onResume==");
		// TODO Auto-generated method stub
		super.onResume();
		isAdded = false;
		if (provider == LocationManager.GPS_PROVIDER) {
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1l, 1l, this);
		} else {
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1l, 1l, this);
		}
		
		loadMap();
		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (lm != null) {
			lm.removeUpdates(this);// 移除那个锅，也就是告诉GPS服务器不要发送GPS信息呢
		}
	}
	
	private void initLocation() {
		Log.d(TAG, "==initLocation==");
		// 使用标准集合，让系统自动选择可用的最佳位置提供器，提供位置
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
		criteria.setAltitudeRequired(false);// 不要求海拔
		criteria.setBearingRequired(false);// 不要求方位
		criteria.setCostAllowed(true);// 允许有花费
		criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
		lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		// 从可用的位置提供器中，匹配以上标准的最佳提供器
		provider = lm.getBestProvider(criteria, true);
		Log.d(TAG, "==provider==" + provider);
		// 获得最后一次变化的位置
		if (provider == LocationManager.GPS_PROVIDER) {
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1l, 1l, this);
		} else {
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1l, 1l, this);
		}
	}
	

	/*
	 * update gps information with location
	 */
	private void updataGpsWidthLocation(Location local) {
		if (local != null && !isAdded) {
			Log.d(TAG, "==updataGpsWidthLocation==");
			Log.e(TAG, "location ==" + local.getLongitude() + "," + local.getLatitude());
			webview.loadUrl(String.format("javascript:addMyPoint(" + local.getLongitude() + "," + local.getLatitude() + ", 3)"));//这里是java端调用webview的JS
//			webview.loadUrl(String.format("javascript:addMulPoint()"));//这里是java端调用webview的JS
			isAdded = true;
		}
	}

	@Override
	public void onLocationChanged(Location local) {
		// 该函数为系统函数，每隔一定的时间便会自动进行调用
		Log.e(TAG, "location change");
		location = local;
		if (isLoaded) {
			updataGpsWidthLocation(local);
		}
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		Log.e(TAG, "statusChanged");
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		Log.e(TAG, "providerenabled");
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		Log.e(TAG, "providerdisabled");
	}
	
}
